<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物业名称">
        <el-select v-model="queryParams.pmid" @visible-change="prosList" clearable>
          <el-option v-for="item in this.properList" :key="item.pmid" :label="item.propertyName"
                     :value="item.pmid"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车场名称">
        <el-select v-model="queryParams.chid" @visible-change="carsList" clearable>
          <el-option v-for="item in this.carList" :key="item.chid" :label="item.chname" :value="item.chid"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="业主姓名" prop="ownerName">
        <el-input
          v-model="queryParams.ownerName"
          placeholder="请输入业主姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业主电话" prop="ownerTel">
        <el-input
          v-model="queryParams.ownerTel"
          placeholder="请输入业主电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门牌号" prop="ownerAddress">
        <el-input
          v-model="queryParams.ownerAddress"
          placeholder="请输入门牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['info:owner_info:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['info:owner_info:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['info:owner_info:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['info:owner_info:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="owner_infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="业主姓名" align="center" prop="ownerName"/>
      <el-table-column label="业主电话" align="center" prop="ownerTel"/>
      <el-table-column label="门牌号" align="center" prop="ownerAddress"/>
      <el-table-column label="所属小区" align="center" prop="propertyManagement.propertyName"/>
      <el-table-column label="停车场" align="center" prop="carsHome.chname"/>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleLook(scope.row)"
            v-hasPermi="['info:owner_info:edit']"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['info:owner_info:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['info:owner_info:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改业主信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="looking">
        <el-form-item label="业主姓名" prop="ownerName">
          <el-input v-model="form.ownerName" placeholder="请输入业主姓名"/>
        </el-form-item>
        <el-form-item label="业主电话" prop="ownerTel">
          <el-input v-model="form.ownerTel" placeholder="请输入业主电话"/>
        </el-form-item>
        <el-form-item label="门牌号" prop="ownerAddress">
          <el-input v-model="form.ownerAddress" placeholder="请输入门牌号"/>
        </el-form-item>
        <el-form-item label="物业名称" >
          <el-select v-model="form.pmid" @visible-change="prosList1" clearable>
            <el-option v-for="item in this.properList1" :key="item.pmid" :label="item.propertyName"
                       :value="item.pmid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车场名称" >
          <el-select v-model="form.chid" @visible-change="carsList1" clearable>
            <el-option v-for="item in this.carList1" :key="item.chid" :label="item.chname"
                       :value="item.chid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOwner_info, getOwner_info, delOwner_info, addOwner_info, updateOwner_info} from "@/api/info/owner_info";
import {listHome, listProperty} from "@/api/ruoyi-home/home";

export default {
  name: "Owner_info",
  data() {
    return {
      looking: false,
      properList: [],
      carList: [],
      properList1: [],
      carList1: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedPropertyManagement: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 业主信息管理表格数据
      owner_infoList: [],
      // 物业管理表格数据
      propertyManagementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ownerName: null,
        ownerTel: null,
        ownerAddress: null,
        pmid: null,
        chid: null
      },
      // 表单参数
      form: {
        propertyManagement: {},
        carsHome: {},
      },
      proId: {
        pmid: null,
      },
        // 表单校验
        rules: {
          code: [
            {required: true, message: "唯一编码,前台使用不能为空", trigger: "blur"}
          ],
          status: [
            {required: true, message: "状态不能为空", trigger: "change"}
          ],
          deleted: [
            {required: true, message: "是否删除不能为空", trigger: "blur"}
          ],
          createTime: [
            {required: true, message: "创建时间不能为空", trigger: "blur"}
          ],
          updateTime: [
            {required: true, message: "修改时间不能为空", trigger: "blur"}
          ],
          ownerName: [
            {required: true, message: "业主姓名不能为空", trigger: "blur"}
          ],
          ownerTel: [
            {required: true, message: "业主电话不能为空", trigger: "blur"}
          ],
          ownerAddress: [
            {required: true, message: "门牌号不能为空", trigger: "blur"}
          ],
          pmid: [
            {required: true, message: "所属小区id不能为空", trigger: "change"}
          ],
        }
      };
    },
      created()
    {
      this.getList();
      this.prosList();
      this.carsList();
      this.prosList1();
      this.carsList1();
    },
    methods: {
      prosList1()
      {
        listProperty().then(res => {
          this.properList1 = res.rows;
          this.form.carsHome.chid = null;
        })
      },
      carsList1()
      {
        this.proId.pmid = this.form.propertyManagement.pmid;
        listHome(this.proId).then(res => {
          this.carList1 = res.rows
        })
      },
      prosList()
      {
        listProperty().then(res => {
          this.properList = res.rows;
          this.queryParams.chid = null;
        })
      },
      carsList()
      {
        listHome(this.queryParams).then(res => {
          this.carList = res.rows
        })
      },
      /** 查询业主信息管理列表 */
      getList()
      {
        this.loading = true;
        listOwner_info(this.queryParams).then(response => {
          console.log(response.rows)
          this.owner_infoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel()
      {
        this.looking = false;
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset()
      {
        this.form = {
          omid: null,
          code: null,
          status: null,
          deleted: null,
          createTime: null,
          updateTime: null,
          ownerName: null,
          ownerTel: null,
          ownerAddress: null,
          pmid: null,
          chid: null,
          remark: null,
          propertyManagement: {
            pmid: null,
            propertyName: null,
          },
          carsHome: {
            chid: null,
            chname: null,
          },
        };
        this.propertyManagementList = [];
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery()
      {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery()
      {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection)
      {
        this.ids = selection.map(item => item.omid)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd()
      {
        this.reset();
        this.looking = false;
        this.open = true;
        this.title = "添加业主信息管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row)
      {
        this.reset();
        const omid = row.omid || this.ids
        getOwner_info(omid).then(response => {
          this.form = response.data;
          this.propertyManagementList = response.data.propertyManagementList;
          this.looking = false;
          this.open = true;
          this.title = "修改业主信息管理";
        });
      },
      handleLook(row)
      {
        this.reset();
        const omid = row.omid || this.ids
        getOwner_info(omid).then(response => {
          this.form = response.data;
          this.propertyManagementList = response.data.propertyManagementList;
          this.looking = true;
          this.open = true;
          this.title = "查看业主信息";
        });
      },
      /** 提交按钮 */
      submitForm()
      {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.propertyManagementList = this.propertyManagementList;
            if (this.form.omid != null) {
              updateOwner_info(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addOwner_info(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row)
      {
        const omids = row.omid || this.ids;
        this.$modal.confirm('是否确认删除业主信息管理编号为"' + omids + '"的数据项？').then(function () {
          return delOwner_info(omids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 物业管理序号 */
      rowPropertyManagementIndex({row, rowIndex})
      {
        row.index = rowIndex + 1;
      },
      /** 复选框选中数据 */
      handlePropertyManagementSelectionChange(selection)
      {
        this.checkedPropertyManagement = selection.map(item => item.index)
      },
      /** 导出按钮操作 */
      handleExport()
      {
        this.download('sys-cars-info/owner_info/export', {
          ...this.queryParams
        }, `owner_info_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
